<!--
 * @Author: Dorange.Wang
 * @Date: 2021-05-21 10:04:41
 * @LastEditors: wangming
 * @LastEditTime: 2021-10-18 15:57:01
 * @Description: file content
-->
<template>
  <el-drawer
    :visible="visible"
    direction="rtl"
    :close-on-press-escape="false"
    :wrapperClosable="false"
    size="60%"
    :show-close="false"
  >
    <div class="b g3 f16 tl" slot="title">
      <div class="ks-row-between ks-row-middle">
        <span>
          {{ options.title || title }}
          <el-select
            v-if="options.showSelector"
            class="language-selector"
            v-model="options.language"
            placeholder="脚本类型"
            @change="handleLanguageChange"
          >
            <el-option
              v-for="(value, key) in SCRIPT_TYPE_ENUM"
              :key="key"
              :value="key"
              :label="value"
            />
          </el-select>
        </span>
        <span class="el-icon-close g9 f26 poi" @click="visible = false"></span>
      </div>
    </div>

    <div class="content">
      <div id="script-editor"></div>
      <div class="footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </div>
  </el-drawer>
</template>

<script>
import * as monaco from "monaco-editor/esm/vs/editor/editor.api.js";
import { SCRIPT_TYPE_ENUM } from "@/utils/enum";
import MonacoEditorHint from "@/hint/monacoeditor";
MonacoEditorHint.init("java");
const defaultLanguage = Object.keys(SCRIPT_TYPE_ENUM)[0];
const defaultOptions = {
  title: "", // 标题
  language: "", // 语言 默认枚举中的第一个
  content: "", // 脚本内容
  autoClose: true, // submit时编辑器自动关闭（否则需要外部手动关闭）
  showSelector: false, // 是否需要语言选择器
};

export default {
  name: "ScriptEditor",
  data() {
    return {
      SCRIPT_TYPE_ENUM,
      visible: false,
      title: "编写脚本",
      options: {
        ...defaultOptions,
      },
      callback: () => {},
    };
  },

  watch: {
    visible(value) {
      if (!value) {
        this._scriptEditor.dispose();
        this._scriptEditor = null;
      }
    },
  },

  methods: {
    async open(options = {}, callback) {
      this.options = {
        ...defaultOptions,
        ...options,
        language: options.language || defaultLanguage,
      };

      this.callback = callback;
      this.visible = true;

      this.initEditor();
    },

    async initEditor() {
      const { content, language } = this.options;
      const editorLang = (
        language === "GROOVY" ? "JAVA" : language
      ).toLowerCase();
      await this.$nextTick();
      this._scriptEditor = monaco.editor.create(
        document.getElementById("script-editor"),
        {
          value: content || "",
          language: editorLang,
        }
      );
    },

    handleLanguageChange(value) {
      const editorLang = (value === "GROOVY" ? "JAVA" : value).toLowerCase();
      monaco.editor.setModelLanguage(this._scriptEditor.getModel(), editorLang);
    },

    close() {
      this.visible = false;
    },

    submit() {
      const error = monaco.editor
        .getModelMarkers()
        .filter((i) => i.code !== "6133" && i.code !== "2339");
      if (error.length > 0) {
        this.$message.error("语法错误" + error[0]?.message);
        return;
      }

      const content = this._scriptEditor.getValue() + "";
      if (this.options.autoClose) {
        this.close();
      }
      this.callback({
        content,
        language: this.options.language,
      });
    },
  },
};
</script>

<style lang="scss" scoped>
$footer-size: 60px;
.content {
  height: 100%;
  #script-editor {
    height: calc(100% - #{$footer-size});
  }
  .footer {
    height: $footer-size;
    border-top: 1px solid #f1f1f1;
    box-sizing: border-box;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    padding-right: 20px;
  }
}

.language-selector {
  margin-left: 20px;
  width: 120px;
}
</style>
